package main

import (
	"context"
	"database/sql"
	"fmt"
	"strings"

	"github.com/gogf/gf/v2/os/gcfg"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	// 读取配置文件
	cfg := gcfg.Instance("configs/config.yaml")
	ctx := context.Background()

	// 获取数据库连接字符串
	dbLink := cfg.MustGet(ctx, "database.default.link", "").String()
	if dbLink == "" {
		fmt.Println("错误：数据库连接字符串未配置")
		return
	}

	// 解析连接字符串
	parts := strings.Split(dbLink, ":")
	if len(parts) < 2 {
		fmt.Println("错误：数据库连接字符串格式错误")
		return
	}

	// 提取驱动名称和DSN
	driver := parts[0]
	dsn := strings.Join(parts[1:], ":")

	// 连接数据库
	db, err := sql.Open(driver, dsn)
	if err != nil {
		fmt.Printf("错误：连接数据库失败: %v\n", err)
		return
	}
	defer db.Close()

	// 测试连接
	if err := db.Ping(); err != nil {
		fmt.Printf("错误：数据库连接测试失败: %v\n", err)
		return
	}

	fmt.Println("数据库连接成功！")

	// 查询video_sessions表结构
	rows, err := db.Query("DESCRIBE video_sessions")
	if err != nil {
		fmt.Printf("错误：查询表结构失败: %v\n", err)
		return
	}
	defer rows.Close()

	fmt.Println("\nvideo_sessions表结构:")
	fmt.Println("+---------------+-------------+------+-----+---------+----------------+")
	fmt.Println("| Field         | Type        | Null | Key | Default | Extra          |")
	fmt.Println("+---------------+-------------+------+-----+---------+----------------+")

	// 遍历结果
	for rows.Next() {
		var field, fieldType, null, key, defaultVal, extra sql.NullString
		err := rows.Scan(&field, &fieldType, &null, &key, &defaultVal, &extra)
		if err != nil {
			fmt.Printf("错误：读取表结构失败: %v\n", err)
			return
		}

		// 格式化输出
		fieldStr := getStringValue(field)
		typeStr := getStringValue(fieldType)
		nullStr := getStringValue(null)
		keyStr := getStringValue(key)
		defaultStr := getStringValue(defaultVal)
		extraStr := getStringValue(extra)

		fmt.Printf("| %-13s | %-11s | %-4s | %-3s | %-7s | %-14s |\n",
			fieldStr, typeStr, nullStr, keyStr, defaultStr, extraStr)
	}
	fmt.Println("+---------------+-------------+------+-----+---------+----------------+")

	// 查询索引信息
	indexRows, err := db.Query("SHOW INDEX FROM video_sessions")
	if err != nil {
		fmt.Printf("错误：查询索引信息失败: %v\n", err)
		return
	}
	defer indexRows.Close()

	fmt.Println("\nvideo_sessions表索引:")
	fmt.Println("+---------------+------------+--------------+--------------+")
	fmt.Println("| Key_name      | Non_unique | Column_name  | Seq_in_index |")
	fmt.Println("+---------------+------------+--------------+--------------+")

	// 遍历索引结果
	for indexRows.Next() {
		var table, nonUnique, keyName, seqInIndex, columnName sql.NullString
		var cardinality, subPart, packed, nullable, indexType, comment, indexComment sql.NullString
		var visible, expression sql.NullString

		err := indexRows.Scan(
			&table, &nonUnique, &keyName, &seqInIndex, &columnName,
			&cardinality, &subPart, &packed, &nullable, &indexType,
			&comment, &indexComment, &visible, &expression,
		)
		if err != nil {
			fmt.Printf("错误：读取索引信息失败: %v\n", err)
			return
		}

		// 格式化输出
		keyNameStr := getStringValue(keyName)
		nonUniqueStr := getStringValue(nonUnique)
		columnNameStr := getStringValue(columnName)
		seqInIndexStr := getStringValue(seqInIndex)

		fmt.Printf("| %-13s | %-10s | %-12s | %-12s |\n",
			keyNameStr, nonUniqueStr, columnNameStr, seqInIndexStr)
	}
	fmt.Println("+---------------+------------+--------------+--------------+")
}

// 辅助函数：从NullString获取值
func getStringValue(ns sql.NullString) string {
	if ns.Valid {
		return ns.String
	}
	return "NULL"
}